<?php

/*
 * Copyright by Sygma Media Inovasi Ω2013
 * 
 * Lead Programmer : Sony K Wibisono,S.Si,M.T.I
 * E-mail : sony@sonywibisono.com
 * 
 *  
 * 
 */
global $is_login;

function _add(){
    $select_parent=  select_menu_parent(0);
    $select_title=select_content_title(0);
    echo "<h2>Add Menu</h2>
        <form action=\"?mod=admin&file=menus\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">
#Auto </td></tr>
<tr><td class=\"field_name\">Caption</td><td class=\"field_object\">
<input type=\"text\" name=\"caption\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Tooltip</td><td class=\"field_object\">
<input type=\"text\" name=\"tooltip\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Url Link</td><td class=\"field_object\">
<input type=\"text\" name=\"url_link\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Parent Menu</td><td class=\"field_object\">
$select_parent</td></tr>
<tr><td class=\"field_name\">Content </td><td class=\"field_object\">
<input type=\"text\" name=\"content_id\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Visibility</td><td class=\"field_object\">
<input type=\"radio\" name=\"visible\" value=\"1\"/>Visible
<input type=\"radio\" name=\"visible\" value=\"0\"/>Invisible </td></tr>
<tr><td class=\"field_name\">Position</td><td class=\"field_object\">
<input type=\"text\" name=\"position\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Module</td><td class=\"field_object\">
<input type=\"text\" name=\"module\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Css Name</td><td class=\"field_object\">
<input type=\"text\" name=\"css_name\" class=\"textbox\"/> </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function _edit(){
    global $db,$id;
    if (!empty($id)){
        $sql="select * from menus where id=$id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
            if ($visible=='1'){
                $checked="checked";
                $unchecked="";
            } else {
                $checked="";
                $unchecked="checked";
            }
            $select_parent=  select_menu_parent($parent_id);
            $select_title=select_content_title($content_id);
            echo "<h2>Update Menu</h2>
        <form action=\"?mod=admin&file=menus\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">
<input type=\"hidden\" name=\"id\" value=\"$id\"/> #</td></tr>
<tr><td class=\"field_name\">Caption</td><td class=\"field_object\">
<input type=\"text\" name=\"caption\" class=\"textbox\" value=\"$caption\" size='40'/> </td></tr>
<tr><td class=\"field_name\">Tooltip</td><td class=\"field_object\">
<input type=\"text\" name=\"tooltip\" class=\"textbox\" value=\"$tooltip\" size='40'/> </td></tr>
<tr><td class=\"field_name\">Url Link</td><td class=\"field_object\">
<input type=\"text\" name=\"url_link\" class=\"textbox\" value=\"$url_link\" size='40'/> </td></tr>
<tr><td class=\"field_name\">Parent Id</td><td class=\"field_object\">
$select_parent</td></tr>
<tr><td class=\"field_name\">Content Id</td><td class=\"field_object\">
$select_title </td></tr>
<tr><td class=\"field_name\">Visibility</td><td class=\"field_object\">
<input type=\"radio\" name=\"visible\" value=\"1\" $checked/>Visible
<input type=\"radio\" name=\"visible\" value=\"0\" $unchecked/>Invisible </td></tr>
<tr><td class=\"field_name\">Position</td><td class=\"field_object\">
<input type=\"text\" name=\"position\" class=\"textbox\" value=\"$position\"/> </td></tr>
<tr><td class=\"field_name\">Module</td><td class=\"field_object\">
<input type=\"text\" name=\"module\" class=\"textbox\" size='40' value=\"$module\"/> </td></tr>
<tr><td class=\"field_name\">Css Name</td><td class=\"field_object\">
<input type=\"text\" name=\"css_name\" class=\"textbox\" size='40' value=\"$css_name\"/> </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
        }
    } else {
        echo "ID tidak boleh kosong!";
    }
}
function _add_process(){
    global $db,$id,$caption,$tooltip,$url_link,$parent_id,$content_id,$visible,$position,$module,$css_name;
    if (!empty($caption)){
        $sql="insert into menus values (null,'$caption','$tooltip','$url_link','$parent_id','$content_id','$visible','$position','$module','$css_name')";
        $db->query($sql);
        refresh_to("?mod=admin&file=menus", "Back to Content Management", 1);
    }
    
}
function _edit_process(){
    global $db,$id,$caption,$tooltip,$url_link,$parent_id,$content_id,$visible,$position,$module,$css_name;
    if (!empty($caption)){
        $sql="update menus set caption='$caption',tooltip='$tooltip',url_link='$url_link',
            parent_id='$parent_id',content_id='$content_id',visible='$visible',
                position='$position',module='$module',css_name='$css_name' 
                    where id=$id";
        $db->query($sql);
        refresh_to("?mod=admin&file=menus", "Back to Content Management", 1);
    }
}
function _delete(){
    global $db,$id;
    if (!empty($id)){
        $sql="delete from contents";
        $db->query($sql);
        refresh_to("?mod=admin&file=menus", "Back to Content Management", 1);
    }
}
function get_max_page(){
    global $db,$pagesize;
    $sql="select count(*) from menus";
    $row=$db->fetch_row($sql);
    if (!empty($row)){
        return floor($row[0]/$pagesize)+1;
    } else return 0;
}
function _default(){
    global $db,$page,$pagesize,$mod,$file;
    if (empty($page)){
        $start=0;
        $page=0;
    } else {
        $start=$page*$pagesize;
    }
    $sql="select * from menus limit $start,$pagesize";
    $result=$db->query($sql);
    $mp=  get_max_page();
     
    echo "<h2>List of Menus</h2>";
    echo "<table cellspacing=0 cellpadding=2>";
    echo "<tr><td colspan='11'>Page No : ";
    for($i=0;$i<$mp;$i++){
        $pname=$i+1;
        echo "<a href=\"?mod=$mod&file=$file&page=$i\">$pname</a> |";
    }
    echo "<a href=\"?mod=$mod&file=menus&action=add\"><img src=\"img/add.png\" title=\"Add New Record\" class='icon_small'/></a></td></tr>";
    echo "<tr><th>Id</th><th>Caption</th><th>Tooltip</th><th>Url Link</th><th>Parent </th><th>Content</th>
        <th>Visible</th><th>Position</th><th>Module</th><th>Css Name</th><th>Action</th></tr>";
    while ($row=$db->fetch_array($result)){
          extract($row);
        echo "<tr><td>$id</td><td>$caption</td><td>$tooltip</td><td>$url_link</td><td>$parent_id</td><td>$content_id</td>
            <td>$visible</td><td>$position</td><td>$module</td><td>$css_name</td><td><a href=\"?mod=$mod&file=$file&action=edit&id=$id\">
        <img class=\"icon_small\" src='img/edit.png' title='Edit'/></a>
        <a href=\"?mod=$mod&file=$file&action=delete&id=$id\"><img class=\"icon_small\" src='img/delete.png' title='Delete'/></a></td></tr>";
    }
    echo "<tr><td colspan='11'>Page No : ";
    for($i=0;$i<$mp;$i++){
        $pname=$i+1;
        echo "<a href=\"?mod=$mod&file=$file&page=$i\">$pname</a> |";
    }
    echo "</tr>";
    echo "</table>";
}
global $action;
if (!isset($is_login)){
    refresh_to("?mod=admin", "You Must be login first...", 2);
} else {
show_menus('header');
echo "<div class=\"sub_content\">";
switch($action){
    case 'add':
        _add();
        break;
    case 'add_process':
        _add_process();
        break;
    case 'edit':
        _edit();
        break;
    case 'edit_process':
        _edit_process();
        break;
    case 'delete':
        _delete();
        break;
    default :
        _default();
}
echo "</div>";
}
?>
